package com.zzp.basepresenter.util;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;

import androidx.annotation.Nullable;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TextUtils {

    private static TextUtils instance;

    public static TextUtils getInstance() {
        if (null == instance) {
            synchronized (TextUtils.class) {
                if (null == instance) {
                    instance = new TextUtils();
                }
            }
        }
        return instance;
    }


    //复制
    public boolean copy(Context context, String copyStr) {
        try {
            //获取剪贴板管理器
            ClipboardManager cm = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
            // 创建普通字符型ClipData
            ClipData mClipData = ClipData.newPlainText("Label", copyStr);
            // 将ClipData内容放到系统剪贴板里。
            cm.setPrimaryClip(mClipData);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /*
     * 文本高亮
     * */
    public String wrapperColor(String raw, String size, String color, String strLeft, String strRight) {
        return "<font size=\"" + size + "\" color=\"" + color + "\">" + strLeft + raw + strRight + "</font>";
    }

    /*
     * 文本高亮
     * */
    public String wrapperColor(String color, String str) {
        return "<font color=\"" + color + "\">" + str + "</font>";
    }

    /*
     * 文本加粗高亮
     * */
    public String wrapperBoldColor(String color, String str) {
        return "<font color=\"" + color + "\">" + "<strong>" + str + "</strong>" + "</font>";
    }

    public boolean isEmpty(@Nullable CharSequence str) {
        return str == null || str.length() == 0 || str.equals("null");
    }


    //字符串中提取数字
    public int priseInt(String s) {
        String regEx = "[^0-9]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(s);
        return Integer.parseInt(m.replaceAll("").trim());
    }
}
